﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
// Copyright (C) 2021 kingerrobot
// 版权所有。 
//
// 文件名：WarehousingImpl
// 文件功能描述：
//
// 创建者：名字 (zzl)
// 时间：2021/12/14 15:13:13
//
// 修改人：
// 时间：
// 修改说明：
// 版本：V1.0.0
//----------------------------------------------------------------*/
#endregion


namespace KingerRobot.MexicoSailunWMS.SpiService;
//WMS.Vulcanization
public class BVulcanizationImpl : IVulcanizationService
{

    private readonly static IRcsEquip _RcsEquip = new BRcsEquip();
    private readonly static IRcsEquipCuring _RcsEquipCuring = new BRcsEquipCuring();
    private readonly static IRcsLocation _RcsLocation = new BRcsLocation();
    private readonly static ITaskCmd _Taskcmd = new BTaskCmd();
    private readonly static ITask _Task = new BTask();
    private readonly static IBakWcsTask _BakWcsTask = new BBakWcsTask();
    private readonly static IWmsOutOrderSummary _WmsOutOrderSummary = new BWmsOutOrderSummary();
    private readonly static IProdMaterial _ProdMaterial = new BProdMaterial();
    private readonly static IWcsEquipLoc _WcsEquipLoc = new BWcsEquipLoc();
    private readonly static IWcsEquipLocHold _WcsEquipLocHold = new BWcsEquipLocHold();
    private readonly static IWmsOutOrderMaster _WmsOutOrderMaster = new BWmsOutOrderMaster();
    private readonly static IProdSku _ProdSku = new BProdSku();

    public List<OutOrderMasterSummarys> GetOutOrderStatus(RequestOrderParm parm)
    {
        var masterSummarys = _WmsOutOrderMaster.GetOutOrderMasterSummaryList();
        var materials = _ProdMaterial.GetMaterialList();
        var equipLocs = _WcsEquipLoc.GetEquipLocList();
        var equips = _RcsEquip.GetEquips();
        var outOrderSummarys = masterSummarys.Join(materials, f => f.MaterialId, s => s.Id, (f, s) => new OutOrderMasterSummarys
        {
            FinishMode = f.FinishMode,
            MaterialId = f.MaterialId,
            MaterialCode = s.MaterialCode,
            MaterialName = s.MaterialName,
            OrderCode = f.OrderCode,
            OrderStatus = f.OrderStatus,
            OrderType = f.OrderType,
            StartLoc = f.StartLoc,
            TargetLoc = f.TargetLoc,
            RealCount = f.RealCount,
            PlanCount = f.PlanCount,
            Remark = f.Remark,
        });
        var equipsAndLoc = equipLocs.Join(equips, f => f.EquipId, s => s.Id, (f, s) => new
        {
            locId = f.LocId,
            equipId = f.EquipId,
            equipName = s.EquipName
        });
        var result = outOrderSummarys.Join(equipsAndLoc, f => f.TargetLoc, s => s.locId, (f, s) => new OutOrderMasterSummarys
        {
            FinishMode = f.FinishMode,
            MaterialId = f.MaterialId,
            MaterialCode = f.MaterialCode,
            MaterialName = f.MaterialName,
            OrderCode = f.OrderCode,
            OrderStatus = f.OrderStatus,
            OrderType = f.OrderType,
            StartLoc = f.StartLoc,
            TargetLoc = f.TargetLoc,
            TargetEquipName = s.equipName,
            RealCount = f.RealCount,
            PlanCount = f.PlanCount,
            Remark = f.Remark,

        }).Where(t => t.OrderCode.Contains(parm.OrderNum)).ToList();
        return result;
    }

    public List<CurRequestPositionBean> GetCurRequestPosition(RequestPositionParm parm)
    {
        var result = new List<CurRequestPositionBean>();

        var equips = _RcsEquip.GetEquips();
        if (!string.IsNullOrWhiteSpace(parm.EquipCode))
        {
            equips = equips.Where(t => t.Id == long.Parse(parm.EquipCode)).ToList();
        }
        var equipIds = equips.Select(t => t.Id).ToList();
        var targetLocs = _WcsEquipLoc.GetEquipLocList().Where(t => equipIds.Contains(t.EquipId)).ToList();
        var locs = _RcsLocation.GetLocations();
        var taskCmds = _Taskcmd.GetTaskCmds().Where(t => t.TaskType == (int)EnumTaskType.Exwarehouse || t.TaskType == (int)EnumTaskType.EMS).ToList();
        var tasks = _Task.GetTaskList().Where(t => t.TaskType == (int)EnumTaskType.Exwarehouse || t.TaskType == (int)EnumTaskType.EMS).ToList();
        result = taskCmds.Join(tasks, f => f.TaskId, s => s.Id, (f, s) => new
        {
            TtargetLoc = s.TargetLoc,
            TargetLoc = f.TargetLoc,
            TaskNo = s.TaskNo,
            CurrentLoc = f.CurrentLoc.GetValueOrDefault(),
            StartLoc = f.StartLoc.GetValueOrDefault()
        }).Join(targetLocs, f => f.TtargetLoc, s => s.LocId, (f, s) => new
        {
            TargetLoc = f.TargetLoc,
            TaskNo = f.TaskNo,
            CurrentLoc = f.CurrentLoc,
            StartLoc = f.StartLoc,
            EquipId = s.EquipId
        }).Join(equips, f => f.EquipId, s => s.Id, (f, s) => new
        {
            EquipName = s.EquipName,
            TargetLoc = f.TargetLoc.GetValueOrDefault(),
            TaskNo = f.TaskNo.GetValueOrDefault(),
            CurrentLoc = f.CurrentLoc,
            StartLoc = f.StartLoc
        }).Join(locs, f => f.TargetLoc, s => s.Id, (f, s) => new
        {
            EquipName = f.EquipName,
            TargetLoc = s.LocName,
            TaskNo = f.TaskNo,
            CurrentLoc = f.CurrentLoc,
            StartLoc = f.StartLoc
        }).Join(locs, f => f.CurrentLoc, s => s.Id, (f, s) => new
        {
            EquipName = f.EquipName,
            TargetLoc = f.TargetLoc,
            TaskNo = f.TaskNo,
            CurrentLoc = s.LocName,
            StartLoc = f.StartLoc
        }).Join(locs, f => f.StartLoc, s => s.Id, (f, s) => new CurRequestPositionBean
        {
            EquipName = f.EquipName,
            TargetLoc = f.TargetLoc,
            TaskNo = f.TaskNo,
            CurrentLoc = f.CurrentLoc,
            StartLoc = s.LocName
        }).ToList();

        if (!string.IsNullOrWhiteSpace(parm.CuringName.Trim()))
        {
            result = result.Where(t => t.EquipName.Contains(parm.CuringName.Trim())).ToList();
        }

        return result;
    }

    public List<RequestOrderCuringBean> GetOrderCuringStep(RequestOrderCuringParm parm)
    {
        var masterSummarys = _WmsOutOrderMaster.GetOutFollowList().OrderBy(t => t.EquipCode);

        var equips = _RcsEquipCuring.GetAllEquipCurings();

        var result = masterSummarys.Select(t => new RequestOrderCuringBean
        {
            Id = t.Id.GetValueOrDefault(),
            EquipId = t.EquipId.GetValueOrDefault(),
            EquipName = t.EquipName,
            EquipCode = t.EquipCode,
            StartLoc = t.LocName,
            StockNum = t.StockNum.GetValueOrDefault(),
            Spec = t.Spec,
            TaskNo = t.TaskNo.GetValueOrDefault(),
            CreateTime = t.CreateTime,
            Step = t.SummaryId.GetValueOrDefault(),
            BarCode = t.SkuCode,
            CurStep = t.CurLocName,
            New = t.Step,
        }).ToList();
        if (!string.IsNullOrWhiteSpace(parm.EquipId))
        {
            result = result.Where(t => t.EquipCode == parm.EquipId).ToList();
        }

        if (!string.IsNullOrWhiteSpace(parm.BarCode))//指定条码
        {
            result = result.Where(t => t.BarCode == parm.BarCode).ToList();
        }

        if (!string.IsNullOrWhiteSpace(parm.CuringName.Trim()))//指定硫化机名称
        {
            result = result.Where(t => t.EquipName == parm.CuringName.Trim()).ToList();
        }

        if (!string.IsNullOrWhiteSpace(parm.Spec))
        {
            result = result.Where(t => t.Spec == parm.Spec.Trim()).ToList();
        }

        result.ForEach(item =>
        {
            var curing = equips.Where(t => t.Id == item.Id).FirstOrDefault();
            var bean = _RcsEquipCuring.GetCuringRLReault(curing);
            if (bean is not null)
            {
                item.EquipMode = bean.Mode;
            }
        });


        return result;
    }
    public List<RequestOrderCuringBean> GetOrderCuringStepV2(RequestOrderCuringParm parm)
    {
        var masterSummarys = _WmsOutOrderMaster.GetOutOrderSummaryList();
        var materials = _ProdMaterial.GetMaterialList();
        var equipLocs = _WcsEquipLoc.GetEquipLocList();
        var equips = _RcsEquip.GetEquips();
        var tasks = _Task.GetTaskList().Where(t => t.TaskType == (int)EnumTaskType.Exwarehouse);
        var skus = _ProdSku.GetProdSkuListByTaskIds(tasks.Select(c => c.SkuId).ToList());
        if (!string.IsNullOrWhiteSpace(parm.EquipId))
        {
            equips = equips.Where(t => t.Id == long.Parse(parm.EquipId)).ToList();
        }
        var locs = _RcsLocation.GetAllLocations();
        var result = masterSummarys.Join(materials, f => f.MaterialId, s => s.Id, (f, s) => new
        {
            summaryId = f.Id,
            targetLoc = f.TargetLoc,
            StartLoc = f.StartLoc,
            Spec = s.Spec,
            CreateTime = f.CreateTime,
            StockNum = GetStock(f.MaterialId.GetValueOrDefault())
        }).GroupJoin(tasks, f => f.summaryId, s => s.SummaryId, (f, s) => new
        {
            SummaryId = f.summaryId.GetValueOrDefault(),
            TargetLoc = f.targetLoc.GetValueOrDefault(),
            StartLoc = f.StartLoc.GetValueOrDefault(),
            Spec = f.Spec,
            StockNum = f.StockNum,
            CreateTime = f.CreateTime,
            Tasks = s
        }).SelectMany(t => t.Tasks.DefaultIfEmpty(), (f, s) => new
        {
            SummaryId = f.SummaryId,
            TargetLoc = f.TargetLoc,
            StartLoc = f.StartLoc,
            StockNum = f.StockNum,
            Spec = f.Spec,
            CreateTime = f.CreateTime,
            Sku = s?.SkuId
        }).GroupJoin(skus, f => f.Sku, s => s.Id, (f, s) => new
        {
            SummaryId = f.SummaryId,
            TargetLoc = f.TargetLoc,
            StockNum = f.StockNum,
            StartLoc = f.StartLoc,
            Spec = f.Spec,
            CreateTime = f.CreateTime,
            Sku = s
        }).SelectMany(t => t.Sku.DefaultIfEmpty(), (f, s) => new
        {
            SummaryId = f.SummaryId,
            TargetLoc = f.TargetLoc,
            StockNum = f.StockNum,
            StartLoc = f.StartLoc,
            Spec = f.Spec,
            CreateTime = f.CreateTime,
            BarCode = s?.SkuCode
        }).Join(equipLocs, f => f.TargetLoc, s => s.LocId, (f, s) => new
        {
            EquipId = s.EquipId,
            StartLoc = f.StartLoc,
            StockNum = f.StockNum,
            Spec = f.Spec,
            CreateTime = f.CreateTime,
            SummaryId = f.SummaryId,
            BarCode = f.BarCode
        }).Join(equips, f => f.EquipId, s => s.Id, (f, s) => new
        {
            EquipName = s.EquipName,
            StartLoc = f.StartLoc,
            StockNum = f.StockNum,
            Spec = f.Spec,
            CreateTime = f.CreateTime,
            SummaryId = f.SummaryId,
            BarCode = f.BarCode
        }).Join(locs, f => f.StartLoc, s => s.Id, (f, s) => new RequestOrderCuringBean
        {
            EquipName = f.EquipName,
            StartLoc = s.LocName,
            StockNum = f.StockNum,
            Spec = f.Spec,
            CreateTime = Convert.ToDateTime(f.CreateTime),
            Step = f.SummaryId,
            BarCode = f?.BarCode,
            CurStep = ""
        }).ToList();

        return result;
    }
    private int GetStock(long materialId)
    {
        var stockNum = _RcsLocation.GetLocations().Where(t => t.LocHasSku == 1 && t.MaterialId == materialId).Count();
        return stockNum;
    }
    private string GetOrderCurStep(long summaryId)
    {
        var scanIds = _RcsEquip.GetEquips().Where(t => t.EquipType == 8).Select(t => t.Id).ToList();
        var scanLocs = _WcsEquipLoc.GetEquipLocList().Where(t => scanIds.Contains(t.EquipId)).Select(t => t.LocId).ToList();
        var task = _Task.GetTasksBySummaryId(summaryId);

        if (task == null)
        {
            return "请料";
        }
        var taskCmd = _Taskcmd.GetTaskCmdByTaskNo(task.TaskNo.GetValueOrDefault());

        if (taskCmd == null)
        {
            return "请料";
        }
        if (taskCmd.CmdType == (int)EnumTaskType.Exwarehouse)
        {
            if (taskCmd.CmdStatus == (int)EnumTaskCmdStatus.Executable)
            {
                return "任务创建";
            }
            else
            {
                return "堆垛机执行";
            }
        }
        else if (taskCmd.CmdType == (int)EnumTaskType.Transportation)
        {
            //是否已经经过角度扫描
            if (scanLocs.Contains(taskCmd.StartLoc))
            {
                return "角度扫描";
            }
            else
            {
                return "线体输送";
            }
        }
        else
        {
            return "Ems小车";
        }
    }
    public int GetOrderStep(long summaryId)
    {
        if (summaryId == 0)
        {
            return 0;
        }
        var scanIds = _RcsEquip.GetEquips().Where(t => t.EquipType == 8).Select(t => t.Id).ToList();
        var scanLocs = _WcsEquipLoc.GetEquipLocList().Where(t => scanIds.Contains(t.EquipId)).Select(t => t.LocId).ToList();
        var task = _Task.GetTasksBySummaryId(summaryId);

        if (task == null)
        {
            return 1;
        }
        var taskCmd = _Taskcmd.GetTaskCmdByTaskNo(task.TaskNo.GetValueOrDefault());

        if (taskCmd == null)
        {
            return 1;
        }
        if (taskCmd.CmdType == (int)EnumTaskType.Exwarehouse)
        {
            if (taskCmd.CmdStatus == (int)EnumTaskCmdStatus.Executable)
            {
                return 2;
            }
            else
            {
                return 3;
            }
        }
        else if (taskCmd.CmdType == (int)EnumTaskType.Transportation)
        {
            //是否已经经过角度扫描
            if (scanLocs.Contains(taskCmd.StartLoc))
            {
                return 5;
            }
            else
            {
                return 4;
            }
        }
        else
        {
            return 6;
        }
    }

    public List<RequestInCrnBean> GetInCrnStep(RequestInCrnParm parm)
    {
        var masterSummarys = _WmsOutOrderMaster.GetInFollowList();

        var result = masterSummarys.Select(t => new RequestInCrnBean
        {
            EquipName = t.EquipName,
            TargetLoc = t.LocName,
            Spec = t.Spec,
            TaskNo = t.TaskNo.GetValueOrDefault(),
            CreateTime = Convert.ToDateTime(t.CreateTime),
            BarCode = t?.SkuCode,
            CurStep = t.TypeDesc
        }).Where(t => t.BarCode is not null ? t.BarCode.Contains(parm.BarCode) : false).ToList();

        return result;
    }

    public List<LocaltionOutBean> GetLocationOutBeans(RequestLocaltionOutParm parm)
    {


        var tasks = _WmsOutOrderMaster.GetLocaltionOutList().Where(t => t.CreateTime >= parm.Date[0] && t.CreateTime <= parm.Date[1]).ToList();
        //var tasks = _WmsOutOrderMaster.GetLocaltionOutList().ToList();
       
        if (!string.IsNullOrWhiteSpace(parm.BarCode))
        {
            tasks = tasks.Where(t => t.ProductCode.Contains(parm.BarCode)).ToList();
        }
        //if (!string.IsNullOrWhiteSpace(parm.EquipId))//根据堆垛机编码，指定堆垛机
        //{
        //    var equipHold = _WcsEquipLocHold.GetEquipLocHolds().Where(t => t.EquipId == long.Parse(parm.EquipId)).Select(t => t.LocId).ToList();
        //    tasks = tasks.Where(t => equipHold.Contains(t.StartLoc)).ToList();
        //}

        if (!string.IsNullOrWhiteSpace(parm.CrnName.Trim()))//根据堆垛机名称，指定堆垛机
        {
            var equips = _RcsEquip.GetAllEquips();
            var equipHolds = _WcsEquipLocHold.GetEquipLocHolds()
                                .Join(equips, f => f.EquipId, s => s.Id, (f, s) => new
                                {
                                    f.LocId,
                                    s.EquipName
                                })
                                .Where(f => f.EquipName == parm.CrnName.Trim())
                                .Select(t => t.LocId).ToList();
            tasks = tasks.Where(t => equipHolds.Contains(t.StartLoc)).ToList();
        }

        var reslust = tasks.Select(t => new LocaltionOutBean
        {
            Barcode = t.ProductCode,
            CreateTime = t.CreateTime,
            OutLoc = t.LocCode,
            OutTime = t.UpdateTime,
            Quality = t.GtStatus == "0" ? "合格" : "不合格",
            Spec = t.Spec,
            TaskNo=t.TaskNo,
            LocName=t.LocName,
            TireLock = t.TireLock == 1 ? "锁" : "未锁",
            TbmExpirationTime=t.TbmExpirationTime,
        }).ToList();
        return reslust;
    }
}



